{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  },
  "orig_nbformat": 4,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.9.4 64-bit"
  },
  "interpreter": {
   "hash": "7ad59aab27c906dbbf0c2e1dfcaac35fb186b016969018ec3a977f4c21569b21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import numpy as np \n",
    "import matplotlib.pyplot as plt \n",
    "from scipy import stats\n",
    "plt.rcParams['font.family'] = 'SimHei'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "        user_id                   timestamp      group landing_page  converted\n",
       "0        851104  2017-01-21 22:11:48.556739    control     old_page          0\n",
       "1        804228  2017-01-12 08:01:45.159739    control     old_page          0\n",
       "2        661590  2017-01-11 16:55:06.154213  treatment     new_page          0\n",
       "3        853541  2017-01-08 18:28:03.143765  treatment     new_page          0\n",
       "4        864975  2017-01-21 01:52:26.210827    control     old_page          1\n",
       "...         ...                         ...        ...          ...        ...\n",
       "294473   751197  2017-01-03 22:28:38.630509    control     old_page          0\n",
       "294474   945152  2017-01-12 00:51:57.078372    control     old_page          0\n",
       "294475   734608  2017-01-22 11:45:03.439544    control     old_page          0\n",
       "294476   697314  2017-01-15 01:20:28.957438    control     old_page          0\n",
       "294477   715931  2017-01-16 12:40:24.467417  treatment     new_page          0\n",
       "\n",
       "[294478 rows x 5 columns]"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>timestamp</th>\n      <th>group</th>\n      <th>landing_page</th>\n      <th>converted</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>851104</td>\n      <td>2017-01-21 22:11:48.556739</td>\n      <td>control</td>\n      <td>old_page</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>804228</td>\n      <td>2017-01-12 08:01:45.159739</td>\n      <td>control</td>\n      <td>old_page</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>661590</td>\n      <td>2017-01-11 16:55:06.154213</td>\n      <td>treatment</td>\n      <td>new_page</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>853541</td>\n      <td>2017-01-08 18:28:03.143765</td>\n      <td>treatment</td>\n      <td>new_page</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>864975</td>\n      <td>2017-01-21 01:52:26.210827</td>\n      <td>control</td>\n      <td>old_page</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>294473</th>\n      <td>751197</td>\n      <td>2017-01-03 22:28:38.630509</td>\n      <td>control</td>\n      <td>old_page</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>294474</th>\n      <td>945152</td>\n      <td>2017-01-12 00:51:57.078372</td>\n      <td>control</td>\n      <td>old_page</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>294475</th>\n      <td>734608</td>\n      <td>2017-01-22 11:45:03.439544</td>\n      <td>control</td>\n      <td>old_page</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>294476</th>\n      <td>697314</td>\n      <td>2017-01-15 01:20:28.957438</td>\n      <td>control</td>\n      <td>old_page</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>294477</th>\n      <td>715931</td>\n      <td>2017-01-16 12:40:24.467417</td>\n      <td>treatment</td>\n      <td>new_page</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n<p>294478 rows × 5 columns</p>\n</div>"
     },
     "metadata": {},
     "execution_count": 23
    }
   ],
   "source": [
    "data = pd.read_csv('./ab_data.csv')\n",
    "data\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "alpha = 0.05\n",
    "beta = 0.05\n",
    "k = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "1.6448536269514722\n2.807033768343811\n"
     ]
    }
   ],
   "source": [
    "#求 z 1-a   合 z 1-β\n",
    "z_alpha = stats.norm.ppf(1-alpha)\n",
    "z_beta = stats.norm.ppf(1-(beta*beta))\n",
    "print(z_alpha)\n",
    "print(z_beta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "0.32356267742526096"
      ]
     },
     "metadata": {},
     "execution_count": 26
    }
   ],
   "source": [
    "#求σ  标准差 测试组   \n",
    "df_test = data.loc[(data.group=='treatment')&(data.landing_page=='new_page'),['converted']]\n",
    "df_test\n",
    "sigema = df_test['converted'].std()\n",
    "sigema"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "# p_a = data[(data.group=='control') & (data.converted == 1)]\n",
    "p_a = len( data[(data.group=='control') & (data.converted == 1)&(data.landing_page=='new_page')])/len( data[(data.group=='control')&(data.landing_page=='new_page')])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "0.12136929460580913"
      ]
     },
     "metadata": {},
     "execution_count": 36
    }
   ],
   "source": [
    "p_a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "p_b = len( data[(data.group=='treatment') & (data.converted == 1)&(data.landing_page=='old_page')])/len( data[(data.group=='treatment')&(data.landing_page=='old_page')])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "0.1272264631043257"
      ]
     },
     "metadata": {},
     "execution_count": 38
    }
   ],
   "source": [
    "p_b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "125756.02441145452"
      ]
     },
     "metadata": {},
     "execution_count": 49
    }
   ],
   "source": [
    "#样本量\n",
    "(p_a*(1-p_a)+p_b*(1-p_b))*(((z_alpha+z_beta)/(p_a-p_b))*((z_alpha+z_beta)/(p_a-p_b)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}